package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.persistence.model.Deptinfos;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.modular.base.SqlMapper;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.EtacardInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchEtacardInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.EtacardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/etaCardInfo")
public class EtaCardInfoController extends BaseController {
    @Autowired
    EtacardService etacardService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private SqlMapper sqlMapper;

    private String PREFIX = "/basicData/etaCard/";

    /**
     * 跳转到积分商城管理
     */
    @RequestMapping("")
    public String etacard(Model model) {
        String tableHead = redisTemplate.opsForList().index(ShiroKit.getUser().getAccount()+"Abbreviation",0)+"";
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "etaCardInfo.html";
    }

    /**
     * 商品管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchEtacardInfo info, HttpServletRequest request) {
        Page<EtacardInfo> page = new PageFactory<EtacardInfo>().defaultPage();
        try {
            info.setStartIndex(page.getSize() * (page.getCurrent() - 1));
            info.setLimitIndex(page.getSize());
            page.setCurrent(0);
            List<EtacardInfo> list = etacardService.selectPageListByQueryGetMap(page, info);
            if (list.size() == 0) {
                info.setStartIndex(0);
                list = etacardService.selectPageListByQueryGetMap(page, info);
            }
            page.setTotal(etacardService.getQueryPagesPointCount(page, info));
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

}